﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace xna_game_engine.source
{
    public class StateMachine
    {
        // Members
        protected State_Base mCurState;
        private StateEngine mStateEngine;
        public enum States
        {
            STATE_MAIN_MENU = 0,
            STATE_PAUSE,
            STATE_GAMEPLAY,
            STATE_CHAR_SELECT,
            STATE_CHAR_INFO,
        }

        // Constructor
        public StateMachine()
        {
            mStateEngine = new StateEngine();

            // Install all of our states
            mStateEngine.InstallState(new State_MainMenu(States.STATE_MAIN_MENU));
            mStateEngine.InstallState(new State_Pause(States.STATE_PAUSE));
            mStateEngine.InstallState(new State_Gameplay(States.STATE_GAMEPLAY));
            mStateEngine.InstallState(new State_CharSelect(States.STATE_CHAR_SELECT));
            mStateEngine.InstallState(new State_CharInfo(States.STATE_CHAR_INFO));
            // Add new states before here

            mCurState = mStateEngine.GetState(States.STATE_MAIN_MENU);
            Transition(mCurState.Id);
        }

        public void HandleEvent(EventManager.Events eve)
        {
            mCurState.HandleEvent(eve);
        }

        public void Transition(StateMachine.States targetStateId)
        {
            State_Base temp = mCurState;
            // TODO: Change current state
            mCurState.Exit();
            mCurState = mStateEngine.GetState(targetStateId);
            if (mCurState == null)
            {
                Console.WriteLine("StateMachine Error: Could not find target state (" +targetStateId + ")");
                Console.WriteLine("Did you forget to install it?");
                mCurState = temp;
            }
            mCurState.Enter();
        }
        public void Update(GameTime gameTime)
        {
            mCurState.Update(gameTime);
        }

        public void Draw()
        {
            mCurState.Draw();
        }

        public State_Base GetState(States stateId)
        {
            return mStateEngine.GetState(stateId);
        }

        // Properties
        public State_Base CurrentState
        {
            get { return mCurState; }
        }
    }
}
